package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.CompanyDao;
import com.cn.wanxi.dao.impl.CompanyImpl;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.model.ResultModel;
import com.cn.wanxi.service.CompanyService;
import com.cn.wanxi.util.JedisTool;
import redis.clients.jedis.Jedis;

/**
 * 1.创建文件
 * 2.写入数据
 * 3.读取数据
 * 4.封装数据
 */
public class CompanyServiceImpl implements CompanyService {
    private CompanyDao companyDao = new CompanyImpl();

    /**
     * 1.首先判断redis里面是否有数据
     * 2.如果有，则取redis
     * 3.如果没有，则取数据库，并把数据存入redis
     *
     * @return
     */
    @Override
    public CompanyModel getCompanyModel() {
        CompanyModel companyModel = new CompanyModel();
//        companyModel.setPhone("aa");
//        String aa="a";
//        下面这个创建了几个对象？
//        String bb=new String("bb");
//        aa.length();

//        判断当前key值是否为空
        boolean isRedis = JedisTool.exists(0, "logo");

        if (isRedis) {
//            数据来源于redis
            companyModel = getJedisData();
            System.out.println("redis");
        } else {
            companyModel = companyDao.getCompanyModel();
//            将companyModel存入到redis
            setJedisData(companyModel);
            System.out.println("mysql");

        }
        return companyModel;
    }

    /**
     * 从redis里面取得数据，并返回
     *
     * @return
     */
    private CompanyModel getJedisData() {
        CompanyModel companyModel = new CompanyModel();
        Jedis jedis = new Jedis();
        companyModel.setLogo(jedis.get("logo"));
        companyModel.setPhone(jedis.get("phone"));
        companyModel.setNet(jedis.get("net"));
        companyModel.setAddress(jedis.get("address"));
        companyModel.setLinePhone(jedis.get("linePhone"));
        companyModel.setEmail(jedis.get("email"));
        companyModel.setVersion(jedis.get("version"));
        return companyModel;
    }

    /**
     * 将数据库取得的数据存入redis
     *
     * @param companyModel
     */
    private void setJedisData(CompanyModel companyModel) {
        Jedis jedis = new Jedis();

//        jedis.set("logo", companyModel.getLogo());
//        jedis.expire("logo",5);
        jedis.setex("logo",5,companyModel.getLogo());
        jedis.set("phone", companyModel.getPhone());
        jedis.set("net", companyModel.getNet());
        jedis.set("email", companyModel.getEmail());
        jedis.set("linePhone", companyModel.getLinePhone());
        jedis.set("address", companyModel.getAddress());
        jedis.set("version", companyModel.getVersion());
    }

    @Override
    public ResultModel add(CompanyModel companyModel) {
        return null;
    }

    @Override
    public ResultModel update(CompanyModel companyModel) {
        int count = companyDao.update(companyModel);
        if (count > 0) {
            JedisTool.delKey(0, "logo");
        }
        return ResultModel.getResultModel(count);
    }

    public static void main(String[] args) {
        new CompanyServiceImpl().update(null);
    }

    @Override
    public ResultModel delete(CompanyModel companyModel) {
        return null;
    }

    @Override
    public ResultModel findAll(CompanyModel companyModel) {
        return null;
    }

    @Override
    public ResultModel findById(CompanyModel companyModel) {
        return null;
    }

    @Override
    public ResultModel getCount(CompanyModel companyModel) {
        return null;
    }
}
